package com.zisuyi.uilibrary.module.activty.redeem

import android.graphics.Color
import android.os.Bundle
import android.view.Gravity
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.RecyclerView
import com.weilele.mvvm.base.helper.*
import com.weilele.mvvm.base.helper.annotation.MvvmVLayoutAdapter
import com.weilele.mvvm.base.helper.annotation.MvvmViewModel
import com.weilele.mvvm.utils.activity.*
import com.weilele.mvvm.utils.file.kvDecode
import com.weilele.mvvm.utils.file.kvEncode
import com.weilele.mvvm.utils.vLaout.VLayoutAdapter
import com.zisuyi.game.sdk.net.bean.UserInfoBean
import com.zisuyi.uilibrary.R
import com.zisuyi.game.sdk.utils.UserData
import com.zisuyi.uilibrary.GameMessage
import com.zisuyi.uilibrary.base.BaseUiActivity
import com.zisuyi.uilibrary.databinding.ActivityRedeemBinding
import com.zisuyi.uilibrary.module.activty.present.PresentActivity
import com.zisuyi.uilibrary.module.dialog.SimpleTextDialog.Companion.simpleTextDialog
import com.zisuyi.uilibrary.module.dialog.redeem.RedeemDialog
import com.zisuyi.uilibrary.module.dialog.redeem.RedeemEmailDialog
import com.zisuyi.uilibrary.view.FontTextView

class RedeemActivity : BaseUiActivity<ActivityRedeemBinding>() {
    companion object {
        fun start(appCompatActivity: AppCompatActivity?) {
            appCompatActivity.navigateTo<RedeemActivity>()
        }
    }


    override fun getTitleBarTitle(): CharSequence? {
        return getResString(R.string.redeem)
    }

    override fun isNeedCoinView(): Boolean {
        return true
    }

    @MvvmViewModel
    private lateinit var redeemViewModel: RedeemViewModel

    @MvvmVLayoutAdapter
    private lateinit var btHelper: VLayoutAdapter<Pair<Int, Int>, ButtonHolder>

    @MvvmVLayoutAdapter
    private lateinit var redeemHelper: VLayoutAdapter<UserInfoBean.RedeemDTO, RedeemHolder>
    private val ad by lazy { GameMessage.createAdImpl(this) }

    override fun getObserverLiveData(): List<LiveDataWrap>? {
        return listOf(redeemViewModel.redeemConditionData wrapObserver ::onRedeemCondition)
    }

    override fun initUi(savedInstanceState: Bundle?) {
        super.initUi(savedInstanceState)
        mBinding.rcvRedeem.adapter = btHelper
        mBinding.rcvRedeem1.adapter = redeemHelper
        checkShowFollowDialog()
        btHelper.setOnInterceptCreateViewHolderListener { self, parent, viewType ->
            ButtonHolder(
                FontTextView(parent.context).apply {
                    gravity = Gravity.CENTER
                    textSize = 18f
                    setPadding(0, 0, 0, dip(4))
                    layoutParams =
                        RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, dip(44))
                            .apply {
                                marginStart = dip(8)
                                marginEnd = marginStart
                                topMargin = marginStart
                                bottomMargin = marginStart
                            }
                }
            )
        }
        btHelper.refresh(
            if (getResBool(R.bool.enable_google_store) == true) {
                mutableListOf(
                    Pair(R.string.present, R.style.AppGrnButton),
                    Pair(R.string.wall, R.style.AppYellowButton),
                )
            } else {
                mutableListOf(
                    Pair(R.string.present, R.style.AppGrnButton)
                )
            }
        )

        btHelper.setOnItemClickListener { _, itemView, position, itemData ->
            when (itemData.first) {
                R.string.present -> {
                    PresentActivity.start(appCompatActivity)
                }
                R.string.wall -> {
                    if (!ad.showOfferWall()) {
                        "Wall Open Fail".toast()
                    }
                }
            }
        }
        redeemHelper.setOnItemClickListener { _, itemView, position, itemData ->
            val condition = redeemViewModel.redeemConditionData.data
            if (condition == null) {
                "You cannot redeem temporarily".toast()
            } else {
                var enableRedeem = true
                itemData.conditions.forEach { c ->
                    var enable = false
                    condition.forEach For@{
                        if (it.key == c.key) {
                            c.complete = it.value
                            if (it.value.toInt() >= c.value.toInt()) {
                                enable = true
                                return@For
                            }
                        }
                    }
                    if (!enable) {
                        enableRedeem = false
                        return@forEach
                    }
                }
                if (UserData.diamond < itemData.diamond) {
                    enableRedeem = false
                }
                if (enableRedeem) {
                    RedeemEmailDialog.show(appCompatActivity, itemData)
                } else {
                    RedeemDialog.show(appCompatActivity, itemData)
                }
            }
        }
    }

    private fun checkShowFollowDialog() {
        val count = kvDecode("showTimes", 0)
        if (count < 3) {
            appCompatActivity.simpleTextDialog {
                this.setTitle("Tips")
                isCancelable = false
                this.buttonText = "GO"
                //Please follow our Youtube channel:
                //https://youtu.be/lS6gegQ1A_k
                this.mBinding.tvContent.textFromSpanBean(
                    mutableListOf(
                        SpanBean("Please follow our Youtube channel: "),
                        SpanBean(
                            "https://youtu.be/x3qyqADeGe8",
                            textColor = Color.BLUE,
                            textClickListener = {
                                goToWeb("https://youtu.be/x3qyqADeGe8")
                            }),
                    )
                )
                setOnCLickCloseListener {
                    kvEncode("showTimes", count + 1)
                    dismiss()
                }
                this.buttonNext.onClick {
                    kvEncode("showTimes", count + 1)
                    goToWeb("https://youtu.be/x3qyqADeGe8")
                    this.dismiss()
                }
            }
        }
    }

    override fun initData() {
        super.initData()
        redeemViewModel.redeemCondition()
    }

    /**
     * 兑换条件
     */
    private fun onRedeemCondition(value: StatusValue<MutableList<UserInfoBean.RedeemDTO.ConditionsDTO>>?) {
        when (value) {
            is StatusValue.Running -> {
                mBinding.svRedeem.showLoadingView()
            }
            is StatusValue.Error -> {
                mBinding.svRedeem.showOtherView(value.nonNullMsg, null, retryText = "RETRY") {
                    redeemViewModel.redeemCondition()
                }
            }
            is StatusValue.Success -> {
                mBinding.svRedeem.showContentView()
                val redeems = UserData.userInfo?.redeem
                if (redeems.isNullOrEmpty()) {
                    mBinding.svRedeem.showOtherView(
                        "You cannot redeem temporarily",
                        null,
                        null,
                        null
                    )
                } else {
                    redeemHelper.refresh(redeems)
                }
            }
        }
    }
}